package cn.yunhe.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.yunhe.biz.UserBiz;
import cn.yunhe.model.Admin;
import net.sf.json.JSONObject;

public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter out=response.getWriter();
		
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		
		String name=request.getParameter("username");
		String pwd=request.getParameter("userpwd");
		
  		
		UserBiz userBiz=new UserBiz();
		Admin admin =userBiz.userLogin(name, pwd);
		
  		
		if (admin!=null) {
			String j="{'name':'"+admin.getName()+"'}";
			JSONObject jo=JSONObject.fromObject(j);
  			out.print(jo);						
		}else{
			out.print("{'fail':''}");
		}
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
